// pages/doctorDetails/index.js
import {
  ApiGetdoctorInfo,
  ApiGetDoctorComment,
  getCollectDoc
} from '../../api/index'
const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload,
} = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl,
    commonUpdate,
    commonUpload,
    active: 1,
    activeNames: ['1'],
    docDataTab: [{
      label: '图文问诊',
      value: '0'
    }],
    index2: 0,
    index1: 0,
    datetabSelect: {},
    dataTab: [{
      name: '问诊',
      value: '0'
    }, {
      name: '介绍',
      value: '1'
    }, {
      name: '评价',
      value: '2'
    }],
    finish: false,
    loading: false,
    jiezhen: false,
    params: {
      size: 10,
      page: 1,
      finish: false,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDoctorInfo(options.id, options.date)
    this.getDocComment(options.id)
    this.data.jiezhen = options.jiezhen == '1' ? true : false
    if (options.shouzhen == 1) {
      this.data.docDataTab.push({
        label: '首诊咨询',
        value: '2'
      })
    }
    /*     this.data.order_type = wx.getStorageSync('order_type') */
    this.setData({
      id: options.id,
      order_type: wx.getStorageSync('order_type'),
      index1: Number(options.index1) == '-1' ? 0 : Number(options.index1),
      consultation_type: options.consultation_type,
      docDataTab: this.data.docDataTab,
      jiezhen: this.data.jiezhen
    })
  },


  selectType(e) {
    let type = e.currentTarget.dataset.v
    this.setData({
      order_type: type
    })
    wx.setStorageSync('order_type', type)
  },

  //收藏该医生
  collectClick(e) {
    let val = e.currentTarget.dataset.v
    let that = this
    getCollectDoc({
      doctor_id: this.data.doctorInfo.id
    }).then(res => {
      that.setData({
        'doctorInfo.is_collect': val
      })
    })
  },


  photoDetailClick(e) {
    /*    this.data.doctorInfo */
    let consultation_type = e.currentTarget.dataset.v
    wx.navigateTo({
      url: `/pages/confirmConsultation/index?doctorInfo=${JSON.stringify(this.data.doctorInfo)}&&consultation_type=${consultation_type}`,
    })
  },

  //获取





  onLoadMore() {
    if (this.data.loading || this.data.params.finish) return
    this.data.params.page += 1
    this.getDocComment(this.data.id)
  },

  //点击对应问诊标题
  handledataTabClick(e) {
    this.setData({
      index2: e.currentTarget.dataset.info
    })
  },
  //点击日期
  DatetabSelect(e) {
    this.setData({
      index1: e.currentTarget.dataset.info,
      datetabSelect: e.currentTarget.dataset.id
    })
    /*     console.log(e) */
    this.getDoctorInfo(this.data.id, e.currentTarget.dataset.id.date, "1")
  },

  getDoctorInfo(e, date, val) {
    wx.showLoading({
      title: '加载中'
    })
    ApiGetdoctorInfo({
      doctor_id: e,
      date
    }).then(res => {
      let val = this.data.docDataTab.find(item => {
        return item.label == '视频问诊'
      })
      if (!val && res.data.doctorInfo.video_status == 1) {
        this.data.docDataTab.push({
          label: '视频问诊',
          value: '1'
        })
      }
      this.setData({
        dateList: res.data.dateList,
        doctorInfo: res.data.doctorInfo,
        scheduleInfo: res.data.scheduleInfo,
        labelList: res.data.labelList,
        docDataTab: this.data.docDataTab
      })


      /*  for (let i = 0; i < res.data.labelList; i++) {
         console.log(i)
       } */

      wx.hideLoading()
    })
  },


  //获取评价列表

  getDocComment(id) {
    let DoctorCommentListDat = []
    this.setData({
      loading: true
    })
    ApiGetDoctorComment({
      doctor_id: id,
      ...this.data.params
    }).then((res) => {
      const data = res.data
      DoctorCommentListDat = this.data.params.page === 1 ? data : this.data.DoctorCommentListDat.concat(data)
      this.data.params.finish = DoctorCommentListDat.length >= res.page.total
      this.setData({
        DoctorCommentListDat,
        loading: false
      })

    })
  },

  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  scheduleInfoClick(e) {
    if (e.currentTarget.dataset.v.number > 0) {
      let dataSelect = {}
      let indexData = this.data.index1 ? this.data.index1 : '0'
      dataSelect = this.data.dateList[indexData]
      let data = e.currentTarget.dataset.v
      /*    this.data.doctorInfo */
      wx.navigateTo({
        url: `/pages/confirmConsultation/index?data=${JSON.stringify(data)}&&doctorInfo=${JSON.stringify(this.data.doctorInfo)}&&dataSelect=${JSON.stringify(dataSelect)}`,
      })
    } else {
      wx.showToast({
        title: "当前无余号",
        icon: "error"
      })
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})